package com.firefightingiot.firefightingiot.support.utils;

import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class  DayUtil {
    //------------------------------------正常数据专用---------------------------------------------
    //获取今天日期
    public static String getToday(){
        Date todayDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String today = simpleDateFormat.format(todayDate);
        return today;
    }

    //获取今天日期
    public static String getTodayAllTime(){
        Date todayDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = simpleDateFormat.format(todayDate);
        return today;
    }

    //获取明天日期
    public static String getTomorrow(){
        Date tomorrowDate = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String tomorrow = simpleDateFormat.format(tomorrowDate);
        return tomorrow;
    }

    //获取八天前日期
    public static String getBeforeEightDay(){
        Date beforeEightDayDate = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 8);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String beforeEightDay = simpleDateFormat.format(beforeEightDayDate);
        return beforeEightDay;
    }
    //------------------------------------正常数据专用---------------------------------------------

    //获取今天日期:格式化:年-月-日
    public static String getTodayForStatistics(String formatStr){
        Date todayDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
        String today = simpleDateFormat.format(todayDate);
        return today;
    }
    //获取昨天日期:格式化:年-月-日
    public static String getYestodayForStatistics(String formatStr){
        Date tomorrowDate = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
        String tomorrow = simpleDateFormat.format(tomorrowDate);
        return tomorrow;
    }
    //获取昨天日期
    public static String getYestoday(){
        Date tomorrowDate = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String tomorrow = simpleDateFormat.format(tomorrowDate);
        return tomorrow;
    }

    //获取前天日期
    public static String getBeforeYestoday(){
        Date tomorrowDate = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 2);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String tomorrow = simpleDateFormat.format(tomorrowDate);
        return tomorrow;
    }


    //获取月初的日期
    public static String getMonthStart(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (1 - index));
        return formatter.format(calendar.getTime());
    }
    //获取月末的日期
    public static String getMonthEnd(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (-index));
        return formatter.format(calendar.getTime());
    }

    /**
     * 获取当月内过去 的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static List<String> getPastMonthDateList() {
        int intervals = DateUtil.dayOfMonth(new Date()) - 1;
        List<String> pastDaysList = new ArrayList<>();
        for (int i = 0; i <intervals; i++) {
            pastDaysList.add(getPastDate(i));
            System.out.println(getPastDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static List<String> getPastDateList(int intervals) {
        List<String> pastDaysList = new ArrayList<>();
        for (int i = 0; i <intervals; i++) {
            pastDaysList.add(getPastDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取未来 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static List<String> getFetureDateList(int intervals) {
        List<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i <intervals; i++) {
            fetureDaysList.add(getFetureDate(i));
        }
        return fetureDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past - 1);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past + 1);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }
    //判断是不是月初
    public static boolean isBeginOfMonth(Date date){
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        if(ca.get(Calendar.DATE) == ca.getActualMinimum (Calendar.DAY_OF_MONTH))
            return true;
        else
            return false;
    }

    //判断是不是每月的第二天
    public static boolean isTwoOfMonth(){
        String now = DateUtil.now();
        String day = now.split(" ")[0].split("-")[2];
        if(day.equals("02"))
            return true;
        else
            return false;

    }
    //判断是不是每月的第几天
    public static boolean isFewOfMonth(String dayNumber){
        String now = DateUtil.now();
        String day = now.split(" ")[0].split("-")[2];
        if(day.equals(dayNumber))
            return true;
        else
            return false;

    }

    //获取几天前的日期
    public static String getBeforeNDay(int n){
        Date tomorrowDate = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * n);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String tomorrow = simpleDateFormat.format(tomorrowDate);
        return tomorrow;
    }

    public static void main(String[] args) throws ParseException {
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println(isBeginOfMonth(formatter.parse("2020-07-02 10:10:10")));
        List<String> timeList = DayUtil.getPastDateList(7);
        timeList.add(0,DayUtil.getTodayForStatistics("yyyy-MM-dd"));
        System.out.println(timeList);
    }
}
